﻿namespace ManagerClinic.CauHinh
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Modules;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;

    public partial class frmDanhSachCauHinh : DevExpress.XtraEditors.XtraForm
    {
        GridCheckMarksSelection gridCheckMarksSelection = null;
        CauHinhBusiness cauHinhBusiness = null;
        CauHinhChiTietBusiness cauHinhChiTietBusiness = null;
        IList<CauHinhEntity> lstCauHinhEntity = null;

        frmCauHinh frm = null;

        public frmDanhSachCauHinh()
        {
            InitializeComponent();
            gridCheckMarksSelection = new GridCheckMarksSelection(gvDanhSachCauHinh);
            cauHinhBusiness = new CauHinhBusiness();
            cauHinhChiTietBusiness = new CauHinhChiTietBusiness();
        }

        void LoadDataGrid(object sender, EventArgs e) {
            lstCauHinhEntity = cauHinhBusiness.GetCauHinhList();
            grdDanhSachCauHinh.DataSource = lstCauHinhEntity;
        }

        private void gvDanhSachCauHinh_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (CauHinhEntity dm in deleteObjects)
            {
                cauHinhChiTietBusiness.DeleteCauHinhChiTiet(dm.MaCauHinh);
                cauHinhBusiness.DeleteCauHinh(dm.MaCauHinh);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            frm = new frmCauHinh((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.cauhinhEvent += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void frmDanhSachCauHinh_Load(object sender, EventArgs e)
        {
            LoadDataGrid(sender, e);
        }

        CauHinhEntity getData() {
            if (gvDanhSachCauHinh.RowCount <= 0) return null;
            return (CauHinhEntity)gvDanhSachCauHinh.GetFocusedRow();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var cauHinhEntity = getData();
            if (cauHinhEntity == null) return;

            frm = new frmCauHinh((int)ConstantManagerClinic.StateInsertOrUpdate.Update,cauHinhEntity);
            frm.cauhinhEvent += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void grdDanhSachCauHinh_DoubleClick(object sender, EventArgs e)
        {
            var cauHinhEntity = getData();
            if (cauHinhEntity == null) return;

            frm = new frmCauHinh((int)ConstantManagerClinic.StateInsertOrUpdate.Update, cauHinhEntity);
            frm.cauhinhEvent += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}